home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_c / lzw4w10.zip / ARCHIVE.C < prev    next >
Text File  |  1994-07-12  |  11KB  |  347 lines

  1. /* archive.c */
  2.  
  3. #include <windows.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "archive.h"
  7. #include "rw_io.h"
  8. #include "lzw4w.h"
  9. #include "display.h"
  10. #include "about.h"
  11. #include "dir_io.h"
  12. #include "databox.h"
  13. #include "ascii.h"
  14. #include "sayerror.h"
  15.  
  16. /*
  17. ** PostMainHandle() is required only for the
  18. ** Shareware version of LZW4W.
  19. */
  20.  
  21. #if __cplusplus
  22. extern "C" void FAR PASCAL PostMainHandle(HWND);
  23. #else
  24. extern void FAR PASCAL PostMainHandle(HWND);
  25. #endif
  26.  
  27. static FARPROC lpProcAbout;
  28. static FARPROC lpProcDataBox;
  29.  
  30. static HANDLE ghInstance;
  31.  
  32. static char FileSpec[MAXEDITTEXT];
  33. static char FileName[15];
  34. static char ArchiveName[MAXEDITTEXT];
  35. static char ExtractName[MAXEDITTEXT];
  36. static char Temp[MAXEDITTEXT];
  37. static char CRLF[3] = {CR,LF,'\0'};
  38.  
  39. int FAR PASCAL Dummy(char Byte)
  40. {
  41.  return(1);
  42. }
  43.  
  44. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  45.           LPSTR lpCmdLine, int nCmdShow)
  46. {
  47.  MSG Message;
  48.  HWND hWnd;
  49.  WNDCLASS WndClass;
  50.  
  51.  ghInstance = hInstance;
  52.  
  53.  if(!hPrevInstance)
  54.    {
  55.     WndClass.cbClsExtra = 0;
  56.     WndClass.cbWndExtra = 0;
  57.     WndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  58.     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  59.     /*WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);*/
  60.     WndClass.hIcon = LoadIcon(hInstance, "ArchiveIcon");
  61.     WndClass.hInstance = hInstance;
  62.     WndClass.lpfnWndProc = WndProc;
  63.     WndClass.lpszClassName = "ARCHIVE";
  64.     WndClass.lpszMenuName = "ArchiveMenu";
  65.     WndClass.style = CS_HREDRAW | CS_VREDRAW;
  66.     RegisterClass(&WndClass);
  67.   }
  68.  
  69.  hWnd = CreateWindow("ARCHIVE", "ARCHIVE", WS_OVERLAPPEDWINDOW,
  70.            CW_USEDEFAULT,  CW_USEDEFAULT,
  71.            8*NCOLS,        12*NROWS+48,
  72.            NULL,           NULL,
  73.            hInstance,      NULL);
  74.  /* show time ! */
  75.  ShowWindow(hWnd, nCmdShow);
  76.  while( GetMessage(&Message,NULL,0,0))
  77.    {TranslateMessage(&Message);
  78.     DispatchMessage(&Message);
  79.    }
  80.  return Message.wParam;
  81. }
  82.  
  83. static CheckError(HWND hWnd,int Code)
  84. {if(Code<0)
  85.   {SayError(hWnd,Code);
  86.    return FALSE;
  87.   }
  88.  else return TRUE;
  89. }
  90.  
  91. static void CopyNameOnly(LPSTR Dest,LPSTR Source)
  92. {int i = 0;
  93.  char c;
  94.  while(1)
  95.    {c = (char) *Source++;
  96.     if((c==':')||(c=='\\')) i = 0;
  97.     else if(c=='.') break;
  98.     else Dest[i++] = c;
  99.    }
  100.  Dest[i] = '\0';
  101. }
  102.  
  103. /* get filename from archive file */
  104.  
  105. static int FetchFilename(LPSTR Ptr)
  106. {int i, k;
  107.  char c;
  108.  /* skip past any leading 0's */
  109.  for(k=0;k<5;k++)
  110.    {i = Reader();
  111.     if(i==-1)
  112.       {ReaderClose();
  113.        return FALSE;
  114.       }
  115.     c = (char)i;
  116.     if(c!='\0') break;
  117.    }
  118.  Ptr[0] = c;
  119.  /* get remainder of name */
  120.  for(k=1;k<15;k++)
  121.    {i = Reader();
  122.     c = (char)i;
  123.     Ptr[k] = c;
  124.     if(c=='\0') break;
  125.     if( (i==-1) || (c<'!') || (c>'z') )
  126.       {DisplayText("Unexpected end of list!\r\n");
  127.        Ptr[k+1] = '\0';
  128.        wsprintf(Temp,"%s [%xH]\r\n",Ptr,c);
  129.        DisplayText(Temp);
  130.        return FALSE;
  131.       }
  132.    }
  133.  return TRUE;
  134. }
  135.  
  136. /********** WndProc ************/
  137.  
  138.  
  139. long FAR PASCAL WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
  140. {int i, k;
  141.  int Code;
  142.  char c;
  143.  HDC hDC;
  144.  HPEN hPen;
  145.  PAINTSTRUCT ps;
  146.  int Flag;
  147.  switch(iMessage)
  148.    {
  149.     case WM_PAINT:
  150.       hDC = BeginPaint(hWnd, &ps);
  151.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  152.       DisplayPaint(hDC,&ps);
  153.       EndPaint(hWnd,&ps);
  154.       break;
  155.  
  156.     case WM_COMMAND:
  157.       switch(wParam)
  158.          {case MSG_ABOUT:
  159.             DialogBox(ghInstance,"AboutBox",hWnd,lpProcAbout);
  160.             break;
  161.  
  162.           case MSG_COMPRESS:
  163.  
  164.             if(!CheckError(hWnd,InitLZW(14))) break;
  165.             /* get archive spec */
  166.             SetDlgInfoString("Enter archive filename");
  167.             if(!DialogBox(ghInstance,"DataBox",hWnd,lpProcDataBox)) break;
  168.             lstrcpy(ArchiveName,GetDlgTextPtr());
  169.             AnsiUpper(ArchiveName);
  170.             if(!WriterOpen(ArchiveName))
  171.                {MessageBox(hWnd,ArchiveName,"Cannot open",MB_ICONEXCLAMATION);
  172.                 break;
  173.                }
  174.             /* get input file specs */
  175.             SetDlgInfoString("Enter file(s) to archive");
  176.             if(!DialogBox(ghInstance,"DataBox",hWnd,lpProcDataBox))break;
  177.             lstrcpy(FileSpec,GetDlgTextPtr());
  178.             /* compress each file in turn */
  179.             for(i=0;;i++)
  180.               {/* get next filename */
  181.                if(i==0) Flag = FindFirst(FileSpec,FileName);
  182.                else Flag = FindNext(FileName);
  183.                /* skip archive file itself */
  184.                AnsiUpper(FileName);
  185.                if(lstrcmp(FileName,ArchiveName)==0) continue;
  186.                /* compress this file */
  187.                if(Flag)
  188.                  {/* write filename to output file */
  189.                   Writer('\0');
  190.                   for(k=0;k<strlen(FileName);k++) Writer(FileName[k]);
  191.                   Writer('\0');
  192.                   /* open input file */
  193.                   if(ReaderOpen(FileName)) DisplayText(FileName);
  194.                   else
  195.                     {MessageBox(hWnd,FileName,"Cannot open",MB_ICONEXCLAMATION);
  196.                      break;
  197.                     }
  198.                  }
  199.                else break;
  200.                /* compress this file */
  201.                if(!CheckError(hWnd,Compress(Reader,Writer))) break;
  202.                ReaderClose();
  203.                DisplayText(" OK\r\n");
  204.               }
  205.             WriterClose();
  206.             DisplayText("<DONE>\r\n");
  207.             if(!CheckError(hWnd,TermLZW())) break;
  208.             break;
  209.  
  210.           case MSG_EXPAND:
  211.  
  212.             if(!CheckError(hWnd,InitLZW(14))) break;
  213.             /* get archive spec */
  214.             SetDlgInfoString("Enter archive filename");
  215.             if(!DialogBox(ghInstance,"DataBox",hWnd,lpProcDataBox)) break;
  216.             lstrcpy(ArchiveName,GetDlgTextPtr());
  217.             AnsiUpper(ArchiveName);
  218.             if(!ReaderOpen(ArchiveName))
  219.                {MessageBox(hWnd,ArchiveName,"Cannot open",MB_ICONEXCLAMATION);
  220.                 break;
  221.                }
  222.             /* expand each file in turn */
  223.             while(TRUE)
  224.               {/* get filename */
  225.                if(!FetchFilename(FileName)) break;
  226.                DisplayText(FileName);
  227.                if(!WriterOpen(FileName))
  228.                  {MessageBox(hWnd,FileName,"Cannot open",MB_ICONEXCLAMATION);
  229.                   break;
  230.                  }
  231.                /* expand this file */
  232.                if(!CheckError(hWnd,Expand(Reader,Writer))) break;
  233.                WriterClose();
  234.                DisplayText(" OK\r\n");
  235.               }
  236.             DisplayText("<DONE>\r\n");
  237.             if(!CheckError(hWnd,TermLZW())) break;
  238.             break;
  239.  
  240.           case MSG_LIST:
  241.  
  242.             if(!CheckError(hWnd,InitLZW(14))) break;
  243.             /* get archive spec */
  244.             SetDlgInfoString("Enter archive filename");
  245.             if(!DialogBox(ghInstance,"DataBox",hWnd,lpProcDataBox)) break;
  246.             lstrcpy(ArchiveName,GetDlgTextPtr());
  247.             AnsiUpper(ArchiveName);
  248.             if(!ReaderOpen(ArchiveName))
  249.                {MessageBox(hWnd,ArchiveName,"Cannot open",MB_ICONEXCLAMATION);
  250.                 break;
  251.                }
  252.             /* list each file in turn */
  253.             while(TRUE)
  254.               {/* get filename */
  255.                if(!FetchFilename(FileName)) break;
  256.                DisplayText(FileName);
  257.                if(!CheckError(hWnd,Expand(Reader,Dummy))) break;
  258.                DisplayText(CRLF);
  259.               }
  260.             DisplayText("<DONE>\r\n");
  261.             if(!CheckError(hWnd,TermLZW())) break;
  262.             break;
  263.  
  264.           case MSG_EXTRACT:
  265.  
  266.             if(!CheckError(hWnd,InitLZW(14))) break;
  267.             /* get archive spec */
  268.             SetDlgInfoString("Enter archive filename");
  269.             if(!DialogBox(ghInstance,"DataBox",hWnd,lpProcDataBox)) break;
  270.             lstrcpy(ArchiveName,GetDlgTextPtr());
  271.             AnsiUpper(ArchiveName);
  272.             if(!ReaderOpen(ArchiveName))
  273.                {MessageBox(hWnd,ArchiveName,"Cannot open",MB_ICONEXCLAMATION);
  274.                 break;
  275.                }
  276.             /* get file name (for extract) */
  277.             SetDlgInfoString("Enter filename to extract");
  278.             if(!DialogBox(ghInstance,"DataBox",hWnd,lpProcDataBox)) break;
  279.             lstrcpy(ExtractName,GetDlgTextPtr());
  280.             AnsiUpper(ExtractName);
  281.             /* examine each file in turn */
  282.             while(TRUE)
  283.               {/* get filename */
  284.                if(!FetchFilename(FileName)) break;
  285.                DisplayText(FileName);
  286.                /* is this the file to extract ? */
  287.                if(lstrcmp(ExtractName,FileName)==0)
  288.                  {/* found file to extract */
  289.                   if(!WriterOpen(FileName))
  290.                     {MessageBox(hWnd,FileName,"Cannot open",MB_ICONEXCLAMATION);
  291.                      break;
  292.                     }
  293.                   /* expand this file */
  294.                   if(!CheckError(hWnd,Expand(Reader,Writer))) break;
  295.                   ReaderClose();
  296.                   WriterClose();
  297.                   DisplayText(" OK\r\n");
  298.                   break;
  299.                  }
  300.                else
  301.                  {/* skip this file */
  302.                   if(!CheckError(hWnd,Expand(Reader,Dummy))) break;
  303.                   DisplayText(" skipping\r\n");
  304.                  }
  305.               }
  306.             DisplayText("<DONE>\r\n");
  307.             if(!CheckError(hWnd,TermLZW())) break;
  308.             break;
  309.  
  310.           case MSG_EXIT:
  311.  
  312.             PostQuitMessage(0);
  313.             break;
  314.          }
  315.       break;
  316.  
  317.     case WM_CREATE:
  318.  
  319.       /* create AboutDlgProc() thunk */
  320.       lpProcAbout = MakeProcInstance((FARPROC)AboutDlgProc,ghInstance);
  321.       /* create DataBoxDlgProc() thunk */
  322.       lpProcDataBox = MakeProcInstance((FARPROC)DataBoxDlgProc,ghInstance);
  323.       /* initialize window display */
  324.       DisplayInit(hWnd);
  325.       /*
  326.       ** You must call PostMainHandle() before attemping to call LZW.
  327.       ** This is required only for the Shareware version of LZW4W.
  328.       */
  329.       PostMainHandle(hWnd);
  330.       if(!CheckError(hWnd,InitLZW(14)))
  331.         {PostQuitMessage(0);
  332.          break;
  333.         }
  334.       TermLZW();
  335.       break;
  336.  
  337.     case WM_DESTROY:
  338.  
  339.       PostQuitMessage(0);
  340.       break;
  341.  
  342.     default:
  343.  
  344.       return( DefWindowProc(hWnd,iMessage,wParam,lParam) );
  345.    }
  346.  return(NULL);
  347. }